package tool

import cats.data.Validated
import implicits.Implicits._
import tool.Pojo.PredictData

/**
 * Created by yz on 23/6/2021
 */
class PredictDataValidTool(data:PredictData) {

  def validLeu = {
    val v=data.leu
    val info="L-亮氨酸"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validIle = {
    val v=data.ile
    val info="L-异亮氨酸"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validVal = {
    val v=data.`val`
    val info="L-缬氨酸"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validPhe = {
    val v=data.phe
    val info="L-苯丙氨酸"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validTyr = {
    val v=data.tyr
    val info="L-酪氨酸"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validFbp = {
    val v=data.fbp
    val info="空腹血糖"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }.andThen{b=>
      val min=0
      Validated.cond(v.toDouble>=min && v.toDouble<=100, true, s"${info}必须处于${min}到100之间！")
    }
  }

  def validHba1c = {
    val v=data.hba1c
    val info="糖化血红蛋白"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }.andThen{b=>
      val min=0
      Validated.cond(v.toDouble>=min && v.toDouble<=100, true, s"${info}必须处于${min}到100之间！")
    }
  }

  def validAge = {
    val age=data.age
    Validated.cond(age.nonBlank, true, s"年龄不能为空！").andThen{b=>
      Validated.cond(age.isInt, true, s"年龄必须为整数！")
    }.andThen{b=>
      Validated.cond(age.toInt>=18 && age.toInt<=100, true, s"年龄必须处于18到100之间！")
    }
  }

  def validSampleId = {
    val v=data.sampleId
    val info="样本号"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！")
  }

}

object PredictDataValidTool{

  def valid(data: PredictData) = {
    val fileValidTool = new PredictDataValidTool(data)
    import fileValidTool._
    validAge.andThen{b=>
      validSampleId
    }.andThen{b=>
      validLeu
    }.andThen{b=>
      validIle
    }.andThen{b=>
      validVal
    }.andThen{b=>
      validPhe
    }.andThen{b=>
      validTyr
    }.andThen{b=>
      validFbp
    }.andThen{b=>
      validHba1c
    }
  }

}
